<template>
  <div class="card">
    <section class="gouwuche">
      <table class="table table-hover">
        <thead>
          <tr>
            <th>
              <input
                type="checkbox"
                @change="handlechange"
                v-model="isAllChange"
                @click="inshow()"
              />
              全选
            </th>
            <th>商品信息</th>
            <th>单价</th>
            <th>数量</th>
            <th>金额</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="(date, index) in arr"
            :key="index"
            :class="{ bgcolor: isActive }"
          >
            <td>
              <input
                type="checkbox"
                v-model="checkgroup"
                :value="date"
                @change="handleLiChange"
              />
              <img :src="date.imges" />
              {{ date.name }}
            </td>
            <td>
              <p>{{ date.colorClass }}</p>
              <p>{{ date.purchaseMethod }}</p>
              <br />
            </td>
            <td>
              <p>
                <b class="danjia">{{ date.price }}元</b>
              </p>
            </td>
            <td>
              <button @click="handleDelclike(date)">-</button>
              <input type="text" :value="date.numbers" />
              <button @click="date.numbers++">+</button>
            </td>
            <td>
              <p>
                <b class="jiner">{{ getSumje(date) }}.00元</b>
              </p>
            </td>
            <td>
              <el-button type="danger" @click="rmove(index)">删除</el-button>
            </td>
          </tr>
        </tbody>
      </table>

      <div class="box">
        <div class="boxa">
          <input
            type="checkbox"
            @change="handlechange"
            v-model="isAllChange"
            @click="inshow()"
          />
          <a href="#">全选</a>
          <a href="#">删除</a>
        </div>
        <div class="boxc">
          <p>
            已选商品：<b class="jiner">{{ checkgroup.length }}件</b>
          </p>
          <p>
            合计（不含运费）：<b class="jiner">￥{{ getSum() }}</b>
          </p>
        </div>
        <div class="boxb">
          <el-button type="success" @click="succ" class="btn"
            >立即付款</el-button
          >
        </div>
      </div>
    </section>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checkgroup: [],
      isAllChange: false,
      isActive: false,
      arr: [
        {
          //商品名
          name: "宏碁(掠夺者)刀锋500SE",
          //图片地址
          imges:
            "https://img12.360buyimg.com/n1/s450x450_jfs/t1/121492/3/23395/115625/620217fdE9c97540d/d7e4ccbeafb13615.jpg",
          //颜色分类
          colorClass: "机身颜色：暗黑black",
          //购买方式
          purchaseMethod: "官方旗舰店",
          //单价
          price: 13999.0,
          //数量
          numbers: 1,
          //金额
          money: 13999.0,
          //索引
          id: "1",
        },
        {
          //商品名
          name: "机械革命旷世",
          //图片地址
          imges:
            "https://img11.360buyimg.com/n1/jfs/t1/199162/23/20959/126567/6242c991E00b7d0ed/b6ffcca9676b69c6.jpg.avif",
          //颜色分类
          colorClass: "机身颜色：酷雅blue",
          //购买方式
          purchaseMethod: "官方旗舰店",
          //单价
          price: 10699.0,
          //数量
          numbers: 1,
          //金额
          money: 10699.0,
          //索引
          id: "1",
        },
        {
          //商品名
          name: "微星msi强袭GE76",
          //图片地址
          imges:
            "https://img12.360buyimg.com/n5/jfs/t1/182675/12/28772/94929/632ad469E141a2bb0/328ac6a0ce9ddb0e.jpg.avif",
          //颜色分类
          colorClass: "机身颜色：酷雅blue",
          //购买方式
          purchaseMethod: "官方旗舰店",
          //单价
          price: 15499.0,
          //数量
          numbers: 1,
          //金额
          money: 15499.0,
          //索引
          id: "1",
        },
      ],
    };
  },

  methods: {
    inshow() {
      this.isActive = !this.isActive;
    },

    //总额
    getSum() {
      var sum = 0;
      for (var i in this.checkgroup) {
        sum += this.checkgroup[i].numbers * this.checkgroup[i].price;
      }
      return sum;
    },

    //单品总额
    getSumje(date) {
      var sum = date.price;
      sum *= date.numbers;
      return sum;
    },

    //判断全选框
    handlechange() {
      if (this.isAllChange) {
        this.checkgroup = this.arr;
      } else {
        this.checkgroup = [];
      }
    },

    //判断全选框
    handleLiChange() {
      if (this.checkgroup.length !== this.arr.length) {
        this.isAllChange = false;
      } else {
        this.isAllChange = true;
      }
    },

    //数量减1
    handleDelclike(date) {
      var numbers = date.numbers--;
      if (numbers === 1) {
        date.numbers = 1;
      }
    },

    //删除
    rmove(index) {
      var that = this;
      this.$confirm("确定删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.arr.splice(index, 1), this.checkgroup.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    
    // 付款
    succ() {
      if (this.checkgroup.length === 0) {
        this.$message({
          type: "danger",
          message: "请选择商品再购买",
        });
      } else {
        this.$alert("支付成功", "付款", {
          confirmButtonText: "确定",
          callback: (action) => {
            this.$message({
              type: "success",
              message: "已成功购买",
            });
          },
        });
      }
    },
  },
};
</script>

<style scoped>
.gouwuche {
  width: 1200px;
  margin: 0 auto;
}
img {
  width: 50px;
  margin: 0 5px;
}
.table {
  border-spacing: 0; /*把单元格间隙设置为0*/
  border-collapse: collapse;
  width: 100%;
  display: flex;
  flex-direction: column;
}
.table tr {
  border: 1px solid #76b900;
}
.table thead {
  background-color: #76b900;
  color: #fff;
}
.table thead tr {
  padding: 15px 25px;
}
.table thead tr {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
}
.table thead th:first-child {
  width: 270px;
}

.table tbody tr {
  width: 100%;
  padding: 0 25px;
}
.table tbody {
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: 18px;
}
.table tbody tr {
  display: flex;
  justify-content: space-between;
}
.table tbody tr td {
  display: flex;
  flex-direction: row;
  justify-content: left;
  align-items: center;
}
.table tbody tr > td:nth-child(1) {
  flex: 1;
}
.table tbody tr > td:nth-child(2) {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.table tbody tr > td:nth-child(4) input {
  width: 50px;
  text-align: center;
}
.table tbody tr > td:nth-child(4) button {
  padding: 0 5px;
  background-color: #76b900;
  color: #fff;
}

td {
  width: 180px;
  padding: 15px 0;
}
th {
  width: 180px;
  text-align: left;
}

/*  */
.box {
  width: 100%;
  background-color: #75b9005e;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.box div {
  display: flex;
  padding: 15px 25px;
  font-size: 18px;
}
</style>